<?php

require_once ('IPaymentService.php');
require_once ('BaseService.php');
require_once 'util/ServiceUtil.php';
require_once 'util/StringUtil.php';
require_once 'dao/PaymentDao.php';

class PaymentService extends BaseService implements IPaymentService {
	public static $NAME = "PaymentService";
	protected $payment;
	public function PaymentService() 
	{
		$this->initializeDB();
		$this->payment = new PaymentDao();
	}
	
	public function create(PaymentDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "INSERT INTO payment(address,card,cid,company,country,email,fax,note,password,status,tel,zipcode) 
								VALUES('$object->address','$object->card',
								'$object->cid','$object->company','$object->country','$object->email',
								'$object->fax','$object->note','md5($object->password)','$object->status',
								'$object->tel','$object->zipcode')";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function delete($id) 
	{
		if($id)
		{
			try
			{
				$this->sql = "DELETE * FROM payment WHERE id='$id'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function read($id) 
	{
		if($id==NULL||$id==-1)
		{
			$this->sql = "SELECT * FROM payment";
		}else
		{
			$this->sql = "SELECT * FROM payment WHERE id='$id'";
		}
		try
		{
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			
			for($i=0;$i<$this->numRows;$i++)
 			{
 				$row = mysql_fetch_object($this->query);
 				
 				$this->payment->address = $row->address;
				$this->payment->card = $row->card;
				$this->payment->cid = $row->cid;
				$this->payment->company = $row->company;
				$this->payment->country = $row->country;
				$this->payment->email = $row->email;
				$this->payment->fax = $row->fax;
				$this->payment->note = $row->note;
				$this->payment->password = $row->password;
				$this->payment->status = $row->status;
				$this->payment->tel = $row->tel;
				$this->payment->zipcode = $row->zipcode;
				
				$this->payment->id = $row->id;
				$this->payment->time = $row->time;
				
				$this->result[] = $this->payment;
 			}
 			
			return $this->result;
			
		}catch(Exception $exc)
		{
			return $exc;
		}
		return NULL;
	}
	
	public function update(PaymentDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "UPDATE TABLE payment SET payment.address='$object->address',
								payment.card='$object->card',payment.cid='$object->cid',payment.company='$object->company',
								payment.country='$object->country',payment.email='$object->email',payment.fax='$object->fax',
								payment.note='$object->note',payment.password='$object->password',payment.status='$object->status',
								payment.tel='$object->tel',payment.zipcode='$object->zipcode'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
}

?>